#include "showdatawidget.h"


ShowDataWidget::ShowDataWidget(QWidget *parent) : QWidget(parent)
{
    titleLabel = new QLabel("查看数据");
    titleLabel->setFixedHeight(20);
    titleLabel->setFrameStyle(QLabel::StyledPanel);
    titleLabel->setAlignment(Qt::AlignCenter);
    idListWidget = new QListWidget;
    nameListWidget = new QListWidget;
    factoryListWidget = new QListWidget;
    priceListWidget = new QListWidget;
    sumListWidget = new QListWidget;
    sellListWidget = new QListWidget;
    lastListWidget = new QListWidget;
    updateButton = new QPushButton("刷新");

    QVBoxLayout* topLayout = new QVBoxLayout;
    topLayout->addSpacing(20);
    topLayout->addWidget(titleLabel);

    QHBoxLayout* listWidgetLayout = new QHBoxLayout;
    listWidgetLayout->addWidget(idListWidget);
    listWidgetLayout->addWidget(nameListWidget);
    listWidgetLayout->addWidget(factoryListWidget);
    listWidgetLayout->addWidget(priceListWidget);
    listWidgetLayout->addWidget(sumListWidget);
    listWidgetLayout->addWidget(sellListWidget);
    listWidgetLayout->addWidget(lastListWidget);

    QGridLayout* mainLayout = new QGridLayout(this);
    mainLayout->addLayout(topLayout, 0, 0, 1, 7);
    mainLayout->addLayout(listWidgetLayout, 1, 0, 4, 7);
    mainLayout->addWidget(updateButton, 5, 6, 1, 1);

    connect(updateButton, SIGNAL(clicked(bool)), this, SLOT(showListWidget()));
    showListWidget();
}

void ShowDataWidget::showListWidget()
{
    idListWidget->clear();
    nameListWidget->clear();
    factoryListWidget->clear();
    priceListWidget->clear();
    sumListWidget->clear();
    sellListWidget->clear();
    lastListWidget->clear();

    idListWidget->addItem("序号");
    nameListWidget->addItem("品牌");
    factoryListWidget->addItem("厂家");
    priceListWidget->addItem("价格/万");
    sumListWidget->addItem("总数/辆");
    sellListWidget->addItem("已售/辆");
    lastListWidget->addItem("余量/辆");

    QSqlQuery query;
    query.exec("select id,name,factory,price,sum,sell,last from brand order by id");
    while(query.next())
    {
        idListWidget->addItem(query.value(0).toString());
        nameListWidget->addItem(query.value(1).toString());
        factoryListWidget->addItem(query.value(2).toString());
        priceListWidget->addItem(query.value(3).toString());
        sumListWidget->addItem(query.value(4).toString());
        sellListWidget->addItem(query.value(5).toString());
        lastListWidget->addItem(query.value(6).toString());
    }
}
